package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.DwaConvertCodeRepository;
import cn.com.connext.scrm.repository.master.DwaGetCouponRepository;
import cn.com.connext.scrm.repository.master.PrizeRepository;
import cn.com.connext.scrm.repository.master.WriteOffCodeRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.TmpMsgRecordService;
import cn.com.connext.scrm.web.rest.WriteOffCodeResource;
import cn.com.connext.scrm.web.rest.vm.DwaIsReceiveVM;
import cn.com.connext.scrm.web.rest.vm.TempSubScribeVm;
import cn.com.connext.scrm.web.rest.vm.WriteOffCodePageVM;
import cn.com.connext.scrm.web.rest.vm.WriteOffCodeVM;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
public class WriteOffCodeService {
    private final Logger logger = LoggerFactory.getLogger(WriteOffCodeResource.class);
    @Autowired
    private WriteOffCodeRepository writeOffCodeRepository;

    public JsonResult save(WriteOffCodeVM vm) {
        JsonResult jsonResult =new JsonResult();
       try {
           logger.info("核销入参：{}", vm.toString());
           if (vm != null) {
               WriteOffCode entity = new WriteOffCode();
               BeanUtils.copyProperties(vm, entity);
               entity.setIsDelete(2);
               entity.setCreateTime(ZonedDateTime.now());
               writeOffCodeRepository.saveAndFlush(entity);
               jsonResult.setState(0);
               jsonResult.setMessage("添加成功");
           }else{
               jsonResult.setState(-1);
               jsonResult.setMessage("添加失败,参数不能为空!");
           }
       }catch (Exception e){
           jsonResult.setState(-1);
           jsonResult.setMessage(e.getMessage());
           logger.error("异常信息：{}",e.getMessage());
       }
       return jsonResult;
    }

    public JsonResult edit(WriteOffCodeVM wocv) {
        JsonResult jsonResult =new JsonResult();
        try {
            if (wocv != null) {
                WriteOffCode entity = null;
                if (wocv.getId() != null) {
                    entity = writeOffCodeRepository.findOne(wocv.getId());
                    entity.setId(wocv.getId());
                } else {
                    jsonResult.setState(-1);
                    jsonResult.setMessage("缺少关键参数");
                    return jsonResult;
                }
                if (StringUtils.isNotEmpty(wocv.getRemark())) {
                    entity.setRemark(wocv.getRemark());
                }
                if (StringUtils.isNotEmpty(wocv.getWriteOffScenario())) {
                    entity.setWriteOffScenario(wocv.getWriteOffScenario());
                }
                if (StringUtils.isNotEmpty(wocv.getWriteOffUser())) {
                    entity.setWriteOffUser(wocv.getWriteOffUser());
                }
                writeOffCodeRepository.saveAndFlush(entity);
                jsonResult.setState(0);
                jsonResult.setMessage("修改成功");
            } else {
                jsonResult.setState(-1);
                jsonResult.setMessage("缺少参数");
            }
        }catch (Exception e){
            jsonResult.setState(-1);
            jsonResult.setMessage(e.getMessage());
            logger.error("异常信息：{}",e.getMessage());
        }
            return jsonResult;
    }
    public JsonResult delete(WriteOffCodeVM vm) {
        JsonResult jsonResult =new JsonResult();
        try {
            logger.info("删除入参：{}", vm.toString());
            if (vm != null && vm.getId() !=null) {
                WriteOffCode entity = writeOffCodeRepository.findOne(vm.getId());
                if(entity !=null){
                    entity.setIsDelete(1);
                }
                writeOffCodeRepository.saveAndFlush(entity);
                jsonResult.setState(0);
                jsonResult.setMessage("删除成功");
            }else{
                jsonResult.setState(-1);
                jsonResult.setMessage("删除失败,参数异常!");
            }
        }catch (Exception e){
            jsonResult.setState(-1);
            jsonResult.setMessage(e.getMessage());
            logger.error("异常信息：{}",e.getMessage());
        }
        return jsonResult;
    }

    public WriteOffCode findById(Long writeOffCodeId) {
        return writeOffCodeRepository.findOne(writeOffCodeId);
    }

    public Page<WriteOffCode> findByPage(WriteOffCodePageVM wocv) {
        PageRequest pageRequest = new PageRequest(wocv.getPage(), wocv.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<WriteOffCode> page = writeOffCodeRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new LinkedList<>();
                Predicate activityName = cb.equal(root.get("isDelete"), 2);
                list.add(activityName);
                Predicate[] pre = new Predicate[list.size()];
                return query.where(list.toArray(pre)).getRestriction();
            }
        }, pageRequest);
        return page;
    }

}
